package com.maimang.platform.quartz.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.quartz.entity.SysJob;
import com.maimang.platform.quartz.service.SysJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname SysJobController
 * @description 调度任务信息操作处理
 * @date 2019-04-16 20:26
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/monitor/job")
@Api(value = "调度任务", description = "quartz调度任务信息操作")
public class SysJobController extends BaseController {

	private String prefix = "monitor/job";

	@Autowired
	private SysJobService jobService;

	@GetMapping("/hello")
	@ResponseBody
	public String sayHello() {
		return "Hello 2019";
	}

	@RequiresPermissions("monitor:job:view")
	@GetMapping()
	@ApiIgnore
	public String job() {
		return prefix + "/job";
	}

	@ApiOperation(value = "任务列表", notes = "SysJob参数为空则查询所有")
	@RequiresPermissions("monitor:job:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysJob job) {
		startPage();
		List<SysJob> list = jobService.selectJobList(job);
		return getDataTable(list);
	}

	@ApiOperation(value = "任务导出", notes = "将任务列表数据导出到Excel")
	@Log(title = "定时任务", businessType = BusinessType.EXPORT)
	@RequiresPermissions("monitor:job:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysJob job) {
		List<SysJob> list = jobService.selectJobList(job);
		ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
		return util.exportExcel(list, "定时任务");
	}

	@ApiOperation(value = "删除任务", notes = "根据传入的id删除任务，参数为字符串类型，示例 id1,id2,id3")
	@Log(title = "定时任务", businessType = BusinessType.DELETE)
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			jobService.deleteJobByIds(ids);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error(e.getMessage());
		}
	}

	/**
	 * 任务详情页
	 *
	 * @param jobId    任务id
	 * @param modelMap 返回数据
	 * @return
	 */
	@ApiIgnore
	@RequiresPermissions("monitor:job:detail")
	@GetMapping("/detail/{jobId}")
	public String detail(@PathVariable("jobId") Long jobId, ModelMap modelMap) {
		modelMap.put("name", "job");
		modelMap.put("job", jobService.selectJobById(jobId));
		return prefix + "/detail";
	}

	/**
	 * 任务调度状态修改
	 */
	@ApiOperation(value = "修改任务状态", notes = "根据传入的任务对象修改任务状态")
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequiresPermissions("monitor:job:changeStatus")
	@PostMapping("/changeStatus")
	@ResponseBody
	public Result changeStatus(SysJob job) {
		job.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(jobService.changeStatus(job));
	}

	/**
	 * 任务调度立即执行一次
	 */
	@ApiOperation(value = "执行任务", notes = "根据传入的任务对象立即执行一次")
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequiresPermissions("monitor:job:changeStatus")
	@PostMapping("/run")
	@ResponseBody
	public Result run(SysJob job) {
		return toResult(jobService.run(job));
	}

	/**
	 * 新增调度
	 */
	@ApiIgnore
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存调度
	 */
	@Log(title = "定时任务", businessType = BusinessType.INSERT)
	@RequiresPermissions("monitor:job:add")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysJob job) {
		job.setCreateUser(ShiroUtils.getLoginName());
		return toResult(jobService.insertJobCron(job));
	}

	/**
	 * 修改调度
	 */
	@ApiIgnore
	@GetMapping("/edit/{jobId}")
	public String edit(@PathVariable("jobId") Long jobId, ModelMap modelMap) {
		modelMap.put("job", jobService.selectJobById(jobId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存调度
	 */
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@RequiresPermissions("monitor:job:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysJob job) {
		job.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(jobService.updateJobCron(job));
	}

	/**
	 * 校验cron表达式是否有效
	 */
	@ApiOperation(value = "校验表达式", notes = "校验传入的任务对象cron表达式是否合法")
	@PostMapping("/checkCronExpressionIsValid")
	@ResponseBody
	public boolean checkCronExpressionIsValid(SysJob job) {
		return jobService.checkCronExpressionIsValid(job.getCronExpression());
	}
}
